package com.jinmdz.app.api.api.service;

import com.jinmdz.app.api.api.model.coffin.CoffinDeadData;
import com.jinmdz.app.api.api.model.common.BusinessCodeData;
import com.jinmdz.app.api.base.BaseService;
import com.jinmdz.app.api.constant.MessageConst;
import com.jinmdz.app.api.wrapper.DictionaryWrapper;
import com.jinmdz.app.dao.dao.CoffinDao;
import com.jinmdz.app.dao.dao.FamilyMemberDao;
import com.jinmdz.app.dao.dao.FuneralBusinessDao;
import com.jinmdz.app.dao.dao.FuneralDeadDao;
import com.jinmdz.app.dao.model.coffin.CoffinConfirmItem;
import com.jinmdz.app.dao.model.coffin.CoffinItem;
import com.jinmdz.app.dao.model.funeral.FamilyMemberItem;
import com.jinmdz.app.dao.model.funeral.FuneralBusinessViewItem;
import com.jinmdz.app.dao.model.funeral.FuneralDeadItem;
import com.jinmdz.fmis.common.util.BeanUtil;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.core.constant.EBusinessType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 遗体装棺service
 *
 * @author JiYanJiang
 * @date 2020-08-24 14:55
 */
@Service("coffinService")
public class CoffinService extends BaseService {
    @Resource
    private CoffinDao coffinDao;

    @Resource
    private FuneralBusinessDao funeralBusinessDao;

    @Resource
    private FuneralDeadDao funeralDeadDao;

    @Resource
    private FamilyMemberDao familyMemberDao;

    @Resource
    private DictionaryWrapper dictionaryWrapper;

    /**
     * 加载遗体装棺页面相关信息
     *
     * @param data
     * @return com.jinmdz.fmis.core.base.BaseResult<com.jinmdz.app.api.api.model.coffin.CoffinDeadData>
     * @author JiYanJiang
     * @date 2020-08-25 13:31
     */
    public BaseResult<CoffinDeadData> loadCoffinDeadWithBusinessCode(BusinessCodeData data) {
        String businessCode = data.getBusinessCode();
        // 通过业务编码获取殡葬信息 判断是否有该业务
        FuneralBusinessViewItem funeralItem = funeralBusinessDao.getFuneralBusinessViewByBusinessCode(businessCode);
        if (DataUtil.isNull(funeralItem) || DataUtil.isNull(funeralItem.getId(), funeralItem.getVersion())) {
            return failure(MessageConst.NO_FUNERAL_CODE);
        }

        CoffinDeadData loadItem = new CoffinDeadData();

        // 获取逝者信息
        FuneralDeadItem funeralDeadItem = funeralDeadDao.getFuneralDeadByBusinessCode(businessCode);
        BeanUtil.copy2Bean(funeralDeadItem, loadItem);
        //更新逝者家属字典
        // 设置字典文本
        dictionaryWrapper.resetDataText(loadItem);

        // 获取家属信息
        FamilyMemberItem familyMemberItem = familyMemberDao.getFamilyMemberByBusinessCode(businessCode);
        BeanUtil.copy2Bean(familyMemberItem, loadItem);

        //获得纸棺信息
        String businessType = EBusinessType.TYPE_OF_COFFIN.getType();
        CoffinItem coffinItem = coffinDao.getCoffinByBusinessCode(businessCode, businessType);
        BeanUtil.copy2Bean(coffinItem, loadItem);

        //判断是否预定纸棺
        if (DataUtil.isNull(coffinItem)) {
            loadItem.setItemName("无预定纸棺");
        }

        //判断是否装棺
        CoffinConfirmItem coffinConfirmItem = coffinDao.getCoffinConfirmByBusinessCode(businessCode);
        if (DataUtil.noNullOrEmpty(coffinConfirmItem)) {
            return failure("已装棺");
        }

        return successData(loadItem);

    }
}
